﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Orchard.ContentManagement;
using Orchard.Core.Contents;
using Orchard.Environment.Extensions;

namespace Downplay.Mechanics.Impulses.Defaults
{
    [OrchardFeature("Downplay.Mechanics.Impulses")]
    public class EditItemImpulse : Impulse
    {
        public EditItemImpulse() : base("Edit","Downplay.Mechanics.Impulses.Defaults","Edit",new[]{Orchard.Core.Contents.Permissions.EditContent })
        {
            IncludeReturnUrl = false;
        }

        public bool IncludeReturnUrl { get; set; }

        public override string Url(System.Web.Mvc.HtmlHelper html, System.Web.Mvc.UrlHelper url, IContent content)
        {
            var metadata = content.ContentItem.ContentManager.GetItemMetadata(content);
            if (metadata.DisplayRouteValues == null)
                return null;

            if (IncludeReturnUrl)
            {
                metadata.EditorRouteValues["returnUrl"] = html.ViewContext.HttpContext.Request.RawUrl;
            }
            return url.Action(
                Convert.ToString(metadata.EditorRouteValues["action"]),
                metadata.EditorRouteValues);
        }
    }
}
